﻿using Hzt_WebApi;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
using WebHzt_Api.Models.Database;

namespace WebHzt_Api.Controllers
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class VoteController : Controller
    {
        private readonly Hzt_WebApiContext _db;
        public VoteController(Hzt_WebApiContext db, RedisHelper client)
        {
            _db = db;
        }
        /// <summary>
        /// 资料上传：查询是否投票
        /// </summary>
        /// <param name="request"></param>
        /// <returns></returns>
        [HttpPost]
        //[Authorize]
        public string GetVote(GetVoteRequest request)
        {
            //var userId = Response.HttpContext.User.Claims.FirstOrDefault(x => x.Type == ClaimTypes.NameIdentifier).Value;
            var Vote = _db.Votes.FirstOrDefault(x => x.ConName == request.ConName);
            if (Vote == null)
            {
                return "否";
            }
            return "是";
        }
        /// <summary>
        /// 资料上传：资料上传接口
        /// </summary>
        /// <param name="Datum"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddData(Datum datum)
        {
            if (datum == null)
                return "数据为空";
            _db.Data.Add(datum);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "数据上传成功";
            }
            return "数据上传失败";

        }
        /// <summary>
        /// 资料上传:审核接口
        /// </summary>
        /// <param name="review"></param>
        /// <returns></returns>
        [HttpPost]
        public string AddReview(string Outcome, int DataId)
        {
            if (string.IsNullOrEmpty(Outcome))
            {
                return "审核结果不能为空";
            }
            if (DataId == null)
            {
                return "上传资料Id不能为空";
            }
            var model = new Review();
            model.Outcome = Outcome;
            model.DataId = DataId;
            _db.Reviews.Add(model);
            var row = _db.SaveChanges();
            if (row > 0)
            {
                return "审核成功";
            }
            return "审核失败";
        }
        /// <summary>
        /// 资料上传：查询审核结果（查询整个表）
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public List<Review> GetReviewsList()
        {
            var list = _db.Reviews.ToList();
            return list;
        }
    }
}
